<?php
class timkiem extends CI_Model{

    public function __construct()
    {
        // Call the Model constructor
        $this->load->database();
    }
    
    public function get_result_all($search_string,$search_type,$search_price_min,$search_price_max,$perpage,$offset)
    {
        $search_string = trim($search_string,'<br>');
        $query = $this->db->query("SELECT sanpham.ID , sanpham.TenSP , loai.id AS 'idLoai', loai.idLoai,gia.Gia
                                   FROM sanpham,loai,gia
                                   WHERE sanpham.idLoai = loai.id
                                   AND sanpham.ID = gia.idSP
                                   AND TenSP LIKE ('%$search_string%')
                                   AND loai.idLoai = $search_type
                                   AND gia.Gia >= $search_price_min 
                                   AND gia.Gia <= $search_price_max
                                   ORDER BY id Desc LIMIT $offset,$perpage");
        return $query->result();                      
    }
	
    public function get_result_all_ev($search_string,$perpage,$offset)
    {
        $search_string = trim($search_string,'<br>');
        $query = $this->db->query("SELECT sanpham.ID , sanpham.TenSP , loai.id AS 'idLoai', loai.idLoai,gia.Gia
                                   FROM sanpham,loai,gia
                                   WHERE sanpham.idLoai = loai.id
                                   AND sanpham.ID = gia.idSP
                                   AND TenSP LIKE ('%$search_string%')
                                   ORDER BY id Desc LIMIT $offset,$perpage");
        return $query->result();                      
    }
	
    
	public function get_result_all_non_price($search_string,$search_type,$perpage,$offset)
    {
        $search_string = trim($search_string,'<br>');
        $query = $this->db->query("SELECT sanpham.ID , sanpham.TenSP , loai.id AS 'idLoai', loai.idLoai,gia.Gia
                                   FROM sanpham,loai,gia
                                   WHERE sanpham.idLoai = loai.id
                                   AND sanpham.ID = gia.idSP
                                   AND TenSP LIKE ('%$search_string%')
                                   AND loai.idLoai = $search_type
                                   ORDER BY id Desc LIMIT $offset,$perpage");
        return $query->result();                      
    }
    
    public function get_row($search_string,$search_type,$search_price_min,$search_price_max)
    {
        $search_string = trim($search_string,'<br>');
        if($search_type == '')
        {
            $query = $this->db->query("SELECT count(sanpham.ID) AS 'numrow'
                                   FROM sanpham,loai,gia
                                   WHERE sanpham.idLoai = loai.id
                                   AND sanpham.ID = gia.idSP
                                   AND TenSP LIKE ('%$search_string%')");
        
        }
        else if($search_price_min == 0 && $search_price_max == 0)
        {
            $query = $this->db->query("SELECT count(sanpham.ID) AS 'numrow'
                                   FROM sanpham,loai,gia
                                   WHERE sanpham.idLoai = loai.id
                                   AND sanpham.ID = gia.idSP
                                   AND TenSP LIKE ('%$search_string%')
                                   AND loai.idLoai = $search_type");
        
        }
        else
        {
            $query = $this->db->query("SELECT count(sanpham.ID) AS 'numrow'
                                   FROM sanpham,loai,gia
                                   WHERE sanpham.idLoai = loai.id
                                   AND sanpham.ID = gia.idSP
                                   AND TenSP LIKE ('%$search_string%')
                                   AND loai.idLoai = $search_type
                                   AND gia.Gia >= $search_price_min 
                                   AND gia.Gia <= $search_price_max");
        
        }
        $num = $query->row();
        return $num->numrow;
    }
}

?>